<?php
/**
 * Abstract.php
 * @author qqinxl
 * Tue Nov 06 14:24:15 GMT 2007
 * 
 * @category  iQin
 * @package  
 * @copyright  Copyright (c) qqinxl
 */
/**
 * 
 * @category   Auction
 * @package Models_Basic
 * @subpackage  Controller
 * @copyright  Copyright (c) qqinxl
 */
abstract class ControllerAction_Abstract extends Zend_Controller_Action {
	
	/**
	 * @var  string
	*/
	protected $_baseUrl;
	/**
	 * @var  string
	*/
	protected $_encode;	
	/**
	 * @var Zend_Config_Xml
	*/
	protected $_config;
	/**
	   * @var Zend_Log
	   */
	protected $_logWriter;
	/**
	 * @var Zend_Cache_Core
	 * @access protected
	*/
	protected $_cache;
	
	/**
	 * @var Zend_Translate_Adapter
	*/
	protected $_translator;
	/**
	   * @var Object_User
	   */
	protected $_loginUser;
	/**
	 * @var Zend_Locale
	 * @access protected
	*/
	protected $_locale;
	/**
	   * System pararm
	   * @var Object_PageInfo
	   */
	protected $_pageInfo ;

	/**
     * Initialize object
     *
     * Called from {@link __construct()} as final step of object instantiation.
     *
     * @return void
     */
	public function init() {
		$this->_init();
	}
	
	protected function _init() {		
		//controller内获取config值
		$this->_config = Zend_Registry::get('config');		
		$this->_logWriter = Zend_Registry::get('log');		
		$this->_cache = Zend_Registry::get('cache');				
		$this->_locale = Zend_Registry::get("locale");
		$this->_translator = Zend_Registry::get("Zend_Translate");			
		$this->_baseUrl = Zend_Registry::get('baseUrl');
		$this->_pageInfo = Zend_Registry::get('pageInfo');		
		$this->_encode = $this->_config->view->encoding;
		$this->_loginUser = Zend_Registry::get('loginUser');	
		
		parent::initView();		
		
		$this->view->config = $this->_config;			
		$this->view->logWriter = $this->_logWriter;		
		$this->view->cache = $this->_cache;		
		$this->view->baseUrl = $this->_baseUrl;
		$this->view->pageInfo = $this->_pageInfo;		
		$this->view->encode = $this->_encode;		
		$this->view->setEncoding($this->_encode);	
		$this->view->loginUser = $this->_loginUser;		
	}
	
	
	/**
	 * 翻译
	 * @param string $string
	 * @param string $type
	 * @return string
	*/
	protected function _($string,$options = null){
		$msg = $this->_translator->_($string);
		if(!empty($options) and is_array($options)){
			foreach ($options as $key=>$value){
				$msg = str_replace ( '{'.$key.'}', $value, $msg );
			}
		}
		return $msg;
	}	
	/**
		 * 在每个action之前的统一动作;该方法必须是public类型
		 * Access level to BasicController_Extend::preDispatch() must be public 
		 * @throws Exception
		 * @return void
		*/
	public function preDispatch(){
		if ($this->getRequest()->isXmlHttpRequest () or $this->_pageInfo->isAjaxHttpRequest()) {
			$this->getHelper ( 'layout' )->disableLayout ();
		}
	}	
	public function postDispatch(){
		if (!$this->getRequest()->isXmlHttpRequest () and !$this->_pageInfo->isAjaxHttpRequest()) {
			$this->getResponse ()->insert ( 'sidebar', $this->view->render ( 'basic/site/sidebar.phtml' ) );
		}
	}
	/**
     * 该函数将用户请求的信息自动转化成Utf-8
     * @return array
	 */
	public function getParams($autoChange = true){
		try{
			$result = $this->_helper->getHelper('PageParams')->getParams($autoChange,$this->_encode);
		}catch (Exception $e){
			throw $e;
		}
		return $result;
	}
	/**
     * 该函数将用户请求的信息自动转化成Utf-8
     * @param string $key 
     * @return array
	 */
	public function getParam($key,$autoChange = true){
		try{
			$result = $this->_helper->getHelper('PageParams')->getParam($key,$autoChange,$this->_encode);
		}catch (Exception $e){
			throw $e;
		}
		return $result;
	}
	/**
     * 该函数将用户请求的信息自动转化成Utf-8
     * @param string $key 
     * @throws
     * @return array
	*/
	public function getPosts($autoChange = true){
		try{
			$result = $this->_helper->getHelper('PageParams')->getPosts($autoChange,$this->_encode);
		}catch (Exception $e){
			throw $e;
		}
		return $result;
	}
	/**
	   * 该函数将用户请求的信息自动转化成Utf-8
	   * @param string $key 
	   * @throws
	   * @return array
	 */
	public function getPost($key,$autoChange = true){
		try{
			$result = $this->_helper->getHelper('PageParams')->getPost($key,$autoChange,$this->_encode);
		}catch (Exception $e){
			throw $e;
		}
		return $result;
	}

	/**
     * 
     * 将$this->_redirect("admin/auth/login");
     * 转化为
     * $this->_redirect("/admin/auth/login");
     * 
     * Redirect to another URL
     *
     * Proxies to {@link Zend_Controller_Action_Helper_Redirector::gotoUrl()}. 
     *
     * @param string $url
     * @param array $options Options to be used when redirecting
     * @return void
     */
	protected function _redirect($url, array $options = array()){
		$url = $this->_helper->getHelper('PageParams')->changeRedirectUrl($url);
		parent::_redirect($url,$options);
	}
	/**
     * Render a view
     *
     * @see Zend_Controller_Response_Abstract::appendBody()
     * @param  string|null $action Defaults to action registered in request object
     * @param  string|null $name Response object named path segment to use; defaults to null
     * @param  bool $noController  Defaults to false; i.e. use controller name as subdir in which to search for view script
     * @return void
     */
	public function render($action = null, $name = null, $noController = false,$autoChange = true){
		$isChange = $this->_helper->getHelper('PageParams')->isNeedChangeEncode($autoChange,$this->_encode);
		if($isChange){
			if (!$this->getInvokeArg('noViewRenderer') && $this->_helper->hasHelper('viewRenderer')) {
				return $this->_helper->viewRenderer->render($action, $name, $noController);
			}
			$view   = $this->initView();
			$script = $this->getViewScript($action, $noController);			
			return $this->getResponse()->appendBody(mb_convert_encoding($view->render($script),$this->_encode,'UTF-8'),$name);
		}else{
			return parent::render($action,$name,$noController);
		}
	}

	public function messageAction(){
		$this->render();
	}
}